package com.shushan.accessibility.miui;

import android.view.accessibility.AccessibilityNodeInfo;

import com.shushan.accessibility.AccessibilityOperator;

/**
 * @ClassName: MiuiOperator
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-07-09
 */
public class MiuiOperator {

    private static final String TAG = "MiuiOperator";

    private static MiuiOperator mInstance = new MiuiOperator();

    public static MiuiOperator getInstance() {
        return mInstance;
    }

    public boolean closeSavePower() {
        AccessibilityOperator.getInstance().getRecycleView();
        return  AccessibilityOperator.getInstance().clickListItem("android:id/list","无限制",1);
    }

    public boolean openComplete() {
        AccessibilityOperator.getInstance().getRecycleView();
        return AccessibilityOperator.getInstance().clickListCheckBox("书山宝",1,true);
    }

    public boolean openFloatManager() {
        AccessibilityOperator.getInstance().getRecycleView();
        boolean result = AccessibilityOperator.getInstance().clickListItem("android:id/list","显示悬浮窗",1);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            AccessibilityOperator.getInstance().getRecycleView();
            return AccessibilityOperator.getInstance().clickByText("允许");
        }
    }

    public boolean openUsageSetting() {
        AccessibilityOperator.getInstance().getRecycleView();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean result = AccessibilityOperator.getInstance().clickListItem("android:id/list","书山宝",1);
        if(!result){
            return false;
        }else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            AccessibilityOperator.getInstance().getRecycleView();
            AccessibilityNodeInfo checkNode = AccessibilityOperator.getInstance().getNodeByClassName("android.widget.CheckBox");
            if(checkNode != null && !checkNode.isChecked()){
                return AccessibilityOperator.getInstance().clickListItem("android:id/list","允许访问使用记录",1);
            }

            return false;
        }
    }

    public boolean enableAdmin() {
        AccessibilityOperator.getInstance().getRecycleView();
        boolean result = AccessibilityOperator.getInstance().clickById("com.android.settings:id/restricted_action");
        if(!result){
            AccessibilityOperator.getInstance().getRecycleView();
            return AccessibilityOperator.getInstance().clickById("com.android.settings:id/action_button");
        }else {
            return true;
        }

    }
}
